package org.summer.lawyer.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.satoken.utils.LoginHelper;
import org.summer.lawyer.domain.LegalDocument;
import org.summer.lawyer.domain.vo.legaldocument.LegalDocumentCreateVo;
import org.summer.lawyer.domain.vo.legaldocument.LegalDocumentUpdateVo;
import org.summer.lawyer.domain.vo.legaldocument.LegalDocumentVo;
import org.summer.lawyer.mapper.LegalDocumentMapper;
import org.summer.lawyer.service.ILegalDocumentService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class LegalDocumentServiceImpl extends ServiceImpl<LegalDocumentMapper, LegalDocument> implements ILegalDocumentService {

    @Override
    public LegalDocumentVo selectLegalDocumentVoByDocType(String docType) {
        LegalDocumentVo vo = new LegalDocumentVo();
        vo.setDocType(docType);
        List<LegalDocumentVo> voList = selectLegalDocumentVoList(vo);
        if (CollUtil.isNotEmpty(voList)) {
            return voList.getFirst();
        }
        return vo;
    }

    /**
     * 查询用户协议
     *
     * @param id 主键
     * @return 用户协议
     */
    @Override
    public LegalDocumentVo selectLegalDocumentVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询用户协议列表
     *
     * @param vo        查询条件
     * @param pageQuery 分页参数
     * @return 用户协议分页列表
     */
    @Override
    public TableDataInfo<LegalDocumentVo> selectPageList(LegalDocumentVo vo, PageQuery pageQuery) {
        LambdaQueryWrapper<LegalDocument> lqw = buildQueryWrapper(vo);
        Page<LegalDocumentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户协议列表
     *
     * @param vo 查询条件
     * @return 用户协议列表
     */
    @Override
    public List<LegalDocumentVo> selectLegalDocumentVoList(LegalDocumentVo vo) {
        LambdaQueryWrapper<LegalDocument> lqw = buildQueryWrapper(vo);
        lqw.orderByDesc(LegalDocument::getVersion);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LegalDocument> buildQueryWrapper(LegalDocumentVo vo) {
        Map<String, Object> params = vo.getParams();
        LambdaQueryWrapper<LegalDocument> lqw = Wrappers.lambdaQuery();
        lqw.eq(vo.getDocType() != null, LegalDocument::getDocType, vo.getDocType());
        lqw.like(StrUtil.isNotBlank(vo.getTitle()), LegalDocument::getTitle, vo.getTitle());
        lqw.like(StrUtil.isNotBlank(vo.getVersion()), LegalDocument::getVersion, vo.getVersion());
        return lqw;
    }

    /**
     * 新增用户协议
     *
     * @param vo 用户协议
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByVo(LegalDocumentCreateVo vo) {
        LegalDocument add = MapstructUtils.convert(vo, LegalDocument.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtil.date());
        add.setUpdateTime(DateUtil.date());
        if(LoginHelper.getLoginUser() != null){
            add.setCreateBy(LoginHelper.getLoginUser().getUserId());
            add.setUpdateBy(LoginHelper.getLoginUser().getUserId());
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            vo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户协议
     *
     * @param vo 用户协议
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByVo(LegalDocumentUpdateVo vo) {
        LegalDocument update = MapstructUtils.convert(vo, LegalDocument.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtil.date());
        if(LoginHelper.getLoginUser() != null){
            update.setUpdateBy(LoginHelper.getLoginUser().getUserId());
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LegalDocument entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用户协议信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean checkVersionUnique(LegalDocumentVo vo) {
        return baseMapper.exists(new LambdaQueryWrapper<LegalDocument>()
                //.eq(LegalDocument::getVersion, vo.getVersion())
                .eq(LegalDocument::getDocType, vo.getDocType())
                .ne(ObjectUtil.isNotNull(vo.getId()), LegalDocument::getId, vo.getId()));
    }
}
